#############################################
#FIGURE 1a - DRAW MAP OF MEMBER RELOCATION
#############################################

#devtools::install_github("UrbanInstitute/urbnmapr")
library(ggplot2)
library(maps)
library(readstata13)
library(tidyverse)
library(urbnmapr)
data<-read.dta13("MapData_107_115_OutState.dta")

pdf(file="MemberMoveMap.pdf", family="Bookman")
p <- ggplot() + 
  geom_polygon(data = urbnmapr::states, mapping = aes(x = long, y = lat, group = group),
               fill = "grey", color = "white") +
  coord_map(projection = "albers", lat0 = 39, lat1 = 45)
p <- p + geom_segment(aes(x=c(data$birthlongitude), y=c(data$birthlatitude), xend=c(data$RepLongitude), yend=c(data$RepLatitude)), size=.2, arrow=arrow(length=unit(.1, "cm")))
p <- p + geom_point(aes(x=c(data$birthlongitude), y=c(data$birthlatitude)), size=.6)
p <- p + theme_void()
p <- p + labs(title="          Patterns of Member Movement")
p
dev.off()

#############################################
#FIGURE 1b - DRAW MAP OF DESTINATION DENSITY
#############################################

library(ggplot2)
library(maps)
library(readstata13)
library(tidyverse)
library(urbnmapr)
data<-read.dta13("MapData_107_115_OutStateDensity.dta")
all_states<-urbnmapr::states
#all_states
head(all_states)
data$state_abbv<-data$Rep_state
Total<-merge(all_states, data, all=TRUE)
Total<-Total[order(Total$order),]
head(Total)

pdf(file="MemberDensityMap.pdf", family="Bookman")
Total %>%
ggplot(mapping=aes(long, lat, group=group, fill=Total)) + 
  scale_fill_gradient(low = "gray98", high = "black",
                      guide = guide_colorbar(title = element_blank())) +
  geom_polygon(color = "#ffffff", size = .25) +
  coord_map(projection = "albers", lat0 = 39, lat1 = 45) + 
  theme_void() +
  theme(legend.position="bottom", legend.key.width=unit(.75, "in")) +
  labs(title = "          Density of Destination States")
dev.off()

